﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using DigiLib;
using System.Security.Cryptography;

public partial class controls_admin_ArticlesEdit : Admin_Controls_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                //base.ModuleByContent(lsbModule, "ARTICLES", 1);
                base.bindDDL(ddlCategories, BO_portal_categories.InsideContent(0, "ARTICLES"), "FULL_CATEGORY_NAME", "CATEGORY_ID");
                loadData();
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);                
            }
        }
    }

    private void loadData()
    {
        //DataRow drow = BO_portal_articles.Search(Id).Rows[0];
        //ddlCategories.SelectedValue = drow["CATEGORY_ID"].ToString();
        //ctImages.Src = "/" + Setting.Path + drow["THUMB_IMG"].ToString();
        //txtFULL_ARTICLE_TITLE.Text = drow["FULL_ARTICLE_TITLE"].ToString();
        //txtTitleEng.Text = drow["FULL_ARTICLE_TITLE_ENG"].ToString();
        //ckContent.Text = drow["ARTICLE_DETAIL"].ToString();
        //ckContentEng.Text = drow["ARTICLE_DETAIL_ENG"].ToString();
        //cboDisplay.Checked = Convert.ToBoolean(drow["IS_DISPLAY_ON_HOME_PAGE"].ToString());
        //cboHot.Checked = Convert.ToBoolean(drow["IS_HOT"].ToString());
        //ObjDT = BO_cms.Join_Tables_Select(Id, "FK_portal_articles_portal_modules", false);
        //foreach (DataRow dr in ObjDT.Rows)
        //{
        //    foreach (ListItem item in lsbModule.Items)
        //    {
        //        if (dr["ID_2"].ToString().Trim().Equals(item.Value.Trim()))
        //            item.Selected = true;
        //        if (Convert.ToInt32(dr["ID_2"]) == Module_ID)
        //            txtSortDisplay.Text = dr["VAR1"].ToString();
        //    }
        //}
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //try
        //{
        //    DataRow drow = BO_portal_articles.Search(Id).Rows[0];
        //    string default_string = string.Empty;
        //    string filename = drow["THUMB_IMG"].ToString();
        //    filename = base.Upload("articles", filename, fulTHUMB_IMG, divMessage, false);
        //    int category_id = Convert.ToInt32(ddlCategories.SelectedValue);
        //    int status = 1;
        //    string details = ckContent.Text;

        //    string titleEng = txtTitleEng.Text.Trim();
        //    string detailsEng = ckContentEng.Text;

        //    bool displayOnHome = cboDisplay.Checked;
        //    bool hot = cboHot.Checked;
        //    int sort_display = Convert.ToInt32(txtSortDisplay.Text.Trim());
        //    string code = drow["ARTICLE_CODE"].ToString();
        //    string strResult = BO_portal_articles.Update(Id, category_id, 1, code,
        //                     default_string, filename, string.Empty, string.Empty, title, titleEng, string.Empty
        //                     , string.Empty, full_article_description, descriptionEng,
        //                     details, detailsEng, m_key, m_desc, tags, Convert.ToInt32(drow["TOTAL_DOWNLOAD"]),
        //                    Convert.ToInt32(drow["TOTAL_VOTE"]), Convert.ToInt32(drow["TOTAL_COMMENT"]), Convert.ToInt32(drow["TOTAL_VIEW"]),
        //                    displayOnHome, hot, sort_display, sort_display, Convert.ToDateTime(drow["PUBLISHED_DATE"]), Convert.ToDateTime(drow["APPROVED_DATE"]),
        //                    Administrator.USER_NAME, Convert.ToDateTime(drow["CREATED_DATE"]), Administrator.USER_NAME, DateTime.Now, Administrator.USER_NAME, status);
        //    if (strResult != "0")
        //    {
        //        divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Cập nhật thành công!");
        //        foreach (ListItem item in lsbModule.Items)
        //        {
        //            if (item.Selected)
        //            {
        //                if (Module_ID == Convert.ToInt32(item.Value))
        //                {
        //                    BO_cms.Join_Tables_Update(Id, Module_ID, "FK_portal_articles_portal_modules", sort_display, 1);
        //                }
        //                else BO_cms.Join_Tables_Insert(Id, Convert.ToInt32(item.Value), "FK_portal_articles_portal_modules", sort_display, 1);
        //            }
        //            else
        //            {
        //                BO_cms.Join_Tables_Delete(Id, Convert.ToInt32(item.Value), "FK_portal_articles_portal_modules");
        //            }
        //        }
        //        foreach (string tag in tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        //        {
        //            if (!string.IsNullOrEmpty(tag))
        //            {
        //                try
        //                {
        //                    int id_1 = Convert.ToInt32(BO_portal_tags.Insert(tag.Trim(), 0, DateTime.Now, Administrator.USER_NAME, DateTime.Now, Administrator.USER_NAME, true));
        //                    if (id_1 > 0) BO_cms.Join_Tables_Insert(id_1, Id, "FK_portal_tags_portal_articles", 0, 0);
        //                }
        //                catch { continue; }
        //            }
        //        }
        //        if (Module_ID > 0)
        //            Response.Redirect("default.aspx?control=articlesmodule&module_id=" + Module_ID.ToString() + "&keyword=" + Keyword);
        //        else
        //            Response.Redirect("default.aspx?control=articles&category_id=" + ddlCategories.SelectedValue + "&" + Request["pager"].ToString() + "=" + Request[Request["pager"].ToString()].ToString() + "&keyword=" + Keyword);
        //    }
        //    else
        //        divMessage.InnerHtml = MessageBox.Error("Lỗi", "Có lỗi trong quá trình xử lý! Đăng tin thất bại!");
        //}
        //catch (Exception ex)
        //{
        //    divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
        //}
    }
}
